package com.t3go.t3_map.business.wait;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.shareware.common.utils.LogExtKt;
import com.t3go.passenger.base.utils.FIvLoader;
import com.t3go.t3_map.R;
import com.t3go.t3_map.entity.Speed;

public class MapWaitAnimView extends RelativeLayout {

    private static final String TAG = "MapWaitAnimView";
    private AnimatorSet mAnimatorSet;
    private Speed mSpeed = Speed.NORMAL;

    public MapWaitAnimView(Context context) {
        this(context, null);
    }

    public MapWaitAnimView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MapWaitAnimView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(CENTER_IN_PARENT);
        ImageView imageView1 = new ImageView(context);
        imageView1.setLayoutParams(layoutParams);
        imageView1.setImageResource(R.drawable.icon_wait_drawable_map_search);

        ImageView imageView2 = new ImageView(context);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(CENTER_IN_PARENT);
        imageView2.setLayoutParams(params);
        FIvLoader.get().setBackground(imageView2, R.string.f_src_icon_lucy_sweep);

        addView(imageView1);
        addView(imageView2);

        initAnimations();
    }

    private void initAnimations() {
        int duration = getDuration(mSpeed);
        LogExtKt.log("", "initAnimations -> duration = " + duration);
        ValueAnimator alphaAnimator = ValueAnimator.ofFloat(1f, 1f, 1f, 0);
        alphaAnimator.setRepeatMode(ValueAnimator.RESTART);
        alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);
        alphaAnimator.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            getChildAt(0).setAlpha(value);
        });

        ValueAnimator scaleAnimator = ValueAnimator.ofFloat(0, 1);
        scaleAnimator.setRepeatMode(ValueAnimator.RESTART);
        scaleAnimator.setRepeatCount(ValueAnimator.INFINITE);
        scaleAnimator.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            getChildAt(0).setScaleX(value);
            getChildAt(0).setScaleY(value);
        });

        ValueAnimator rotateAnimator = ValueAnimator.ofFloat(0, 360);
        rotateAnimator.setRepeatMode(ValueAnimator.RESTART);
        rotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
        rotateAnimator.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            getChildAt(1).setRotation(value);
        });

        mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(alphaAnimator, scaleAnimator, rotateAnimator);
        mAnimatorSet.setInterpolator(new LinearInterpolator());
        mAnimatorSet.setDuration(duration);
    }

    public void startAnim() {
        LogExtKt.log(TAG, "startAnim");
        if (mAnimatorSet != null && mAnimatorSet.isRunning()) {
            return;
        }
        if (mAnimatorSet != null) {
            mAnimatorSet.start();
        }
    }

    public void cancelAnim() {
        LogExtKt.log(TAG, "cancel");
        if (mAnimatorSet != null) {
            mAnimatorSet.cancel();
        }
    }

    public void startAnimWithRate(Speed speed) {
        if (mSpeed != speed) {
            cancelAnim();
        }
        mSpeed = speed;
        if (mAnimatorSet != null && mAnimatorSet.isRunning()) {
            return;
        }
        initAnimations();
        startAnim();
    }

    private int getDuration(Speed speed) {
        if (speed == Speed.FAST) {
            return 1000;
        } else if (speed == Speed.DOUBLE) {
            return 700;
        }
        return 1500;
    }

}
